package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.Date;

import com.bo.Order;
import com.dao.OrderDao;
import com.db.util.DBUtil;

public class OrderDaoImpl implements OrderDao{
	public Order insert(Order order) throws Exception{
		String insertInto = "insert into t_order(product_id, count, unit_price, total_price, contact_name, phone, deliver_address, create_time, user_id) values(?,?,?,?,?,?,?)";
		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		try {
			int i=0;
			ps = conn.prepareStatement(insertInto);
			ps.setLong(++i, order.getData().get(0).getProductId());
			ps.setInt(++i, order.getData().get(0).getCount());
			ps.setBigDecimal(++i, order.getData().get(0).getUnitPrice());
			ps.setBigDecimal(++i, order.getData().get(0).getTotalPrice());
			ps.setString(++i, order.getContactName());
			ps.setString(++i, order.getPhone());
			ps.setString(++i, order.getDeliverAddress());
			ps.setTimestamp(++i, new Timestamp(new Date().getTime()));
			ps.setLong(++i, order.getUserId());
			ps.execute();
			order.setOrderId(new Long(DBUtil.getLastInsertId(conn)));
		} catch (Exception e) {
			throw e;
		} finally {
			DBUtil.closeAll(null, ps, conn);
		}
		return order;
	}
}
